import Mock from 'mockjs'
import * as _ from 'lodash-es'

// 自定义拓展
/**
 * 生成多层tree结构的列表数据
 * @param template Mockjs数据模板
 */
function treeCallback(template: any, depth = 0, maxDepth = 5, maxChildren = 4, minChildren = 2, sort = 0) {
	const treeNodeList = []
	const topLevelNumber = 2 // 最顶层节点数量限制
	let index = 0
	while (index < topLevelNumber) {
		const tempTemplate = { ...template }
		// 设置当前节点的 sort
		tempTemplate.sort = sort
		if (depth < maxDepth) {
			const childrenDepth = depth + 1
			tempTemplate[`children|${minChildren}-${maxChildren}`] = treeCallback(
				template,
				childrenDepth,
				maxDepth,
				maxChildren,
				minChildren,
				sort + 1 // 所有子节点的排序值都是相同的
			)
			console.log(tempTemplate)
		} else {
			tempTemplate.children = []
		}
		treeNodeList[index++] = tempTemplate
	}
	return treeNodeList
}

Mock.Random.extend({
	diyTreeNode: treeCallback,
})

export const getTreeList = () => {
	return Mock.mock({
		code: 200,
		message: 'create tree data',
		data: Mock.Random.diyTreeNode({
			key: `@guid`,
			title: `@cname`,
			code: '@guid',
			email: '@email',
			ip: '@ip',
			city: '@city',
			url: '@url',
		}),
	})
}
